//
//  SegmentTextView.swift
//  xgwjinterconnection
//
//  Created by WangWeiShuo on 2025/8/19.
//

import SwiftUI

struct SegmentTextView: View {
    // 选项数组
    var options: [String]
    // 选中索引绑定
    @Binding var selectedIndex: Int
    // 自定义高度
    var height: CGFloat = 50
    
    var radius: CGFloat = 10
    
    var body: some View {
        GeometryReader { geometry in
            ZStack(alignment: .leading) {
                // 背景
                RoundedRectangle(cornerRadius: radius)
                    .fill(Color("color_gray_f3f3f3"))
                
                // 选中指示器
                RoundedRectangle(cornerRadius: radius)
                    .fill(Color.blue)
                    .frame(width: geometry.size.width / CGFloat(options.count), height: height)
                    .offset(x: CGFloat(selectedIndex) * (geometry.size.width / CGFloat(options.count)))
                    .animation(.spring(), value: selectedIndex) // 滑动动画
                
                // 选项按钮
                HStack(spacing: 0) {
                    ForEach(0..<options.count, id: \.self) { index in
                        Button(action: {
                            selectedIndex = index
                        }) {
                            VStack(){
                                Spacer()
                                Text("\(index + 1)")
                                    .foregroundColor(selectedIndex == index ? .white : .black)
                                    
                                Spacer()
                                
                                Text(options[index])
                                    .foregroundColor(selectedIndex == index ? .white : .black)
                                Spacer()
                            }
                            .frame(width: geometry.size.width / CGFloat(options.count), height: height)
                        }
                    }
                }
            }
            .frame(height: height)
        }
    }
}
